﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace M
{
	/// <summary>
	/// 友情类
	/// </summary>
	public static class Util
	{
		/// <summary>
		/// 根据服务器设置返回字符串长度 简体中文为 全角2 半角1
		/// </summary>
		/// <returns></returns>
		public static int GetStringLength(string str)
		{
			return GetStringLength(str, Encoding.Default);
		}

		/// <summary>
		/// 根据指定编码返回字符串长度
		/// </summary>
		/// <param name="str"></param>
		/// <param name="encoding"></param>
		/// <returns></returns>
		public static int GetStringLength(string str, Encoding encoding)
		{
			MValidator.CheckForNullReference("String", str);
			return encoding.GetBytes(str).Length;
		}

		/// <summary>
		/// 根据指定编码截断字符串，末尾有可能会出现乱码
		/// </summary>
		/// <param name="str">字符串</param>
		/// <param name="tarLength">目标编码长度</param>
		/// <param name="encoding">编码</param>
		/// <returns></returns>
		public static string TruncateString(string str, int tarLength, Encoding encoding)
		{
			MValidator.CheckForNullReference("String", str);
			// 简单判断是否超出长度，如果未超出则直接返回
			if (GetStringLength(str, encoding) <= tarLength)
				return str;
			// 转换为流，强制截断
			byte[] bys = encoding.GetBytes(str);
			return encoding.GetString(bys, 0, (tarLength - 2 < 0 ? 0 : tarLength - 2));
		}

		/// <summary>
		/// 分割字符串
		/// </summary>
		/// <param name="content">要分割的内容</param>
		/// <param name="splitter">分割标志</param>
		/// <returns></returns>
		public static string[] SplitString(string content, string splitter)
		{
			if (splitter == null || splitter.Length == 0)
				return new string[1] { content };

			char[] _splitter = new char[splitter.Length];
			for (int i = 0; i < splitter.Length; i++)
				_splitter[i] = splitter[i];

			int lastIndex = 0;
			List<string> returns = new List<string>();
			bool isFound = true;

			for (int i = 0; i < content.Length; i++)
			{
				isFound = true;
				if (content.Length - i < _splitter.Length)
				{
					returns.Add(content.Substring(lastIndex, content.Length - lastIndex));
					break;
				}

				for (int j = 0; j < _splitter.Length; j++)
				{
					if (content[i + j] != _splitter[j])
					{
						isFound = false;
						break;
					}
				}
				if (!isFound)
					continue;
				returns.Add(content.Substring(lastIndex, i - lastIndex));
				i += _splitter.Length - 1;
				lastIndex = i + 1;
				if (i == content.Length - 1)
					returns.Add("");
			}
			if (returns.Count == 0)
				returns.Add(content);
			string[] finReturn = returns.ToArray();
			returns.Clear();
			return finReturn;
		}


		/// <summary> 转半角的函数(DBC case) </summary>
		/// <param name="value">要转换成半角的字符串</param>
		/// <returns>半角字符串</returns>
		///<remarks>
		///全角空格为12288，半角空格为32
		///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
		///</remarks>
		public static string ToDBC(string value)
		{
			MValidator.CheckForEmptyString("value", value);
			char[] c = value.ToCharArray();
			for (int i = 0; i < c.Length; i++)
			{
				if (c[i] == 12288)
				{
					c[i] = (char)32;
					continue;
				}
				if (c[i] > 65280 && c[i] < 65375)
					c[i] = (char)(c[i] - 65248);
			}
			return new string(c);
		}

		/// <summary>
		/// 转全角的函数(SBC case)
		/// </summary>
		/// <param name="value">要转成全角的字符串</param>
		/// <returns>全角字符串</returns>
		///<remarks>
		///全角空格为12288，半角空格为32
		///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
		///</remarks>
		public static string ToSBC(string value)
		{
			MValidator.CheckForEmptyString("value", value);
			//半角转全角：
			char[] c = value.ToCharArray();
			for (int i = 0; i < c.Length; i++)
			{
				if (c[i] == 32)
				{
					c[i] = (char)12288;
					continue;
				}
				if (c[i] < 127)
					c[i] = (char)(c[i] + 65248);
			}
			return new string(c);
		}

		/// <summary>
		/// Object转换为String
		/// </summary>
		/// <param name="objValue"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static string ObjectToString(object objValue, string defaultValue)
		{
			try
			{
				return objValue.ToString();
			}
			catch
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Object转换为Int32
		/// </summary>
		/// <param name="objValue">要转换的对象</param>
		/// <param name="defaultValue">转换的缺省值</param>
		/// <returns></returns>
		public static int ObjectToInt(object objValue, int defaultValue)
		{
			if (!(objValue == null || objValue.ToString().Length == 0))
			{
				int defaultHold = defaultValue;
				if (!Int32.TryParse(objValue.ToString(), out defaultValue))
					defaultValue = defaultHold;
			}
			return defaultValue;
		}

		/// <summary>
		/// Object转换为float
		/// </summary>
		/// <param name="objValue">要转换的对象</param>
		/// <param name="defaultValue">转换的缺省值</param>
		/// <returns></returns>
		public static float ObjectToFloat(object objValue, float defaultValue)
		{
			if (!(objValue == null || objValue.ToString().Length == 0))
			{
				float defaultHold = defaultValue;
				if (!float.TryParse(objValue.ToString(), out defaultValue))
					defaultValue = defaultHold;
			}
			return defaultValue;
		}

		/// <summary>
		/// Object转换为Double
		/// </summary>
		/// <param name="objValue">要转换的对象</param>
		/// <param name="defaultValue">转换的缺省值</param>
		/// <returns></returns>
		public static double ObjectToDouble(object objValue, double defaultValue)
		{
			if (!(objValue == null || objValue.ToString().Length == 0))
			{
				double defaultHold = defaultValue;
				if (!Double.TryParse(objValue.ToString(), out defaultValue))
					defaultValue = defaultHold;
			}
			return defaultValue;
		}
	}
}
